package com.xiongmingzhu.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiongmingzhu.entity.Student;
import com.xiongmingzhu.entity.StudentClass;
import com.xiongmingzhu.mapper.ClassMapper;
import com.xiongmingzhu.mapper.StudentMapper;
import com.xiongmingzhu.service.StudentService;
import com.xiongmingzhu.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.StrUtil;
import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private  StudentMapper studentMapper;

    @Autowired
    private ClassMapper classMapper;
    /**
     * 分页条件查询
     * @param pageNo 当前页号
     * @param pageSize 页面大小
     * @param student 查询信息
     * @return 分页信息
     */
    public PageResult<Student> loadList(Integer pageNo,Integer pageSize,Student student){
        PageResult<Student> pageResult = new PageResult<>();
        Page<Student> page = new Page<>(pageNo,pageSize);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(student != null){
            if(StrUtil.isNotBlank(student.getStudentName())){
                queryWrapper.like("name",student.getStudentName());
            }
            if(StrUtil.isNotBlank(student.getStudentPhone())){
                queryWrapper.like("phone",student.getStudentPhone());
            }
            if(student.getStudentAge() !=null){
                queryWrapper.eq("age",student.getStudentAge());
            }
            if(StrUtil.isNotBlank((student.getStudentClass()))){
                queryWrapper.eq("studentClass",student.getStudentClass());
            }
        }
        IPage<Student> result = studentMapper.selectPage(page,queryWrapper);
        pageResult.setRows(result.getRecords());
        pageResult.setTotal(result.getTotal());
        return  pageResult;
    }

    /**
     * 加载所有课程
     * @return 课程信息列表
     */
    public List<StudentClass> loadAllClass(){
        return  classMapper.selectList(null);
    }

    /**
     * add学生
     * @param student 学生对象
     * @return 影响的行数
     */
    @Override
    public int addStudent(Student student) {
        return studentMapper.insert(student);
    }
}
